package cn.cs.controller;

import cn.cs.common.Dto;
import cn.cs.common.DtoUtil;
import cn.cs.common.EmptyUtils;
import cn.cs.pojo.UserAddress;
import cn.cs.service.UserAddressService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/userAddress")
public class UserAddressController {
    @Resource
    private UserAddressService userAddressService;

    /****
     * 保存用户地址
     * @param request
     * @param
     * @return
     */
    @RequestMapping("/wxApi/registerAddress")
    @ResponseBody
    public Dto registerAddress(HttpServletRequest request, HttpServletResponse response) throws Exception {
        return userAddressService.registerAddress(request);
    }

    /****
     * 查寻用户地址信息
     * @param request
     * @param
     * @return
     */
    @RequestMapping("/wxApi/getUserAddress")
    @ResponseBody
    public Dto<UserAddress> getUserAddress(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String userId = request.getParameter("userId");
        Map<String,Object> map = new HashMap<String,Object>();

        if(EmptyUtils.isNotEmpty(userId)){
            map.put("userId",userId);
        }else{
            map.put("userId","0000000");
        }
        return DtoUtil.returnSuccess("获取成功", userAddressService.getUserAddressListById(map));
    }


    /**
     * 查询默认地址
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping("/wxApi/getUserAddressDefault")
    @ResponseBody
    public Dto<UserAddress> getUserAddressDefault(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String userId = request.getParameter("userId");
        if(EmptyUtils.isEmpty(userId)){
            userId = "0000000";
        }
        Map<String,Object> map1 = new HashMap<String,Object>();
        map1.put("userId",userId);
        List<UserAddress> list = userAddressService.getUserAddressListById(map1);
        if(list.size() <= 1){
            return DtoUtil.returnSuccess("获取成功", list);
        }
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("address_type_id","1");
        map.put("userId",userId);
        return DtoUtil.returnSuccess("获取成功", userAddressService.getUserAddressListById(map));
    }



    /*
    * 更改用户地址
    *
    * */
    @RequestMapping("/wxApi/updateUserAddress")
    @ResponseBody
    public Dto<UserAddress> UserAddress(HttpServletRequest request){
        Dto dto=null;

        try {
            dto=userAddressService.updateUserAddress(request);
        }catch (Exception e){
            e.printStackTrace();
            return DtoUtil.returnFail("更改状态失败","0002");
        }
        return DtoUtil.returnDataSuccess("true,0000");
    }

  /*
    * 删除用户地址
    * */
    @RequestMapping("/wxApi/deleteUserAddressById")
    @ResponseBody
    public Dto<UserAddress> DeleteUserAddressById(HttpServletRequest request){

        String id=request.getParameter("id");
        try {
            userAddressService.itriptxDeleteUserAddressById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("更改状态失败","0002");
        }

        return DtoUtil.returnDataSuccess("true,0000");
        }

    /*
     * 设为默认
     *
     * */
    @RequestMapping("/wxApi/updateAddressTypeId")
    @ResponseBody
    public Dto updateAddressTypeId(HttpServletRequest request)throws Exception{
        Dto dto=null;
        return dto=userAddressService.updateAddressTypeId(request);
    }
    }








